﻿/*
* Copyright (c) 2008 michiyasu wada
* http://www.seyself.com/
* 
* Distributed under The MIT License.
* [http://www.opensource.org/licenses/mit-license.php]
*/


class com.seyself.utils.XMLUtil
{
    
    private function XMLUtil()
    {
        
    }
    
    public static function setExtentions():Void
    {
        XMLNode.prototype.getElementById = function( elementId:String ):XML
        {
            function _getElementById( node:XMLNode, elementId:String ):XML
            {
                var children:Array = node.childNodes;
                var len:Number = children.length;
                var _node = null;
                for(var i=0;i<len;i++){
                    var _id:String = children[i].attributes["id"];
                    if( _id == elementId ){
                        return children[i];
                    } else {
                        _node = _getElementById( children[i], elementId );
                        if( _node ){ return _node; }
                    }
                }
                return _node;
            }
            return _getElementById( this, elementId );
        }
        
        XMLNode.prototype.getElementsByTagName = function( tagName:String ):Array
        {
            function _getElementsByTagName( node:XMLNode, tagName:String , elements:Array ):Array
            {
                var children:Array = node.childNodes;
                var len:Number = children.length;
                for(var i=0;i<len;i++){
                    var _tagname:String = children[i].nodeName;
                    if( _tagname == tagName ){
                        elements.push( children[i] );
                    }
                    elements = _getElementsByTagName( children[i], tagName, elements );
                }
                return elements;
            }
            var elements:Array = [];
            elements = _getElementsByTagName( this, tagName, elements );
            return elements;
        }
        
        XMLNode.prototype.getElementsHaveAttr = function( name:String ):Array
        {
            function _getElementsHaveAttr( node:XMLNode, name:String , elements:Array ):Array
            {
                var children:Array = node.childNodes;
                var len:Number = children.length;
                for(var i=0;i<len;i++){
                    var attr:String = children[i].attributes;
                    for(var val in attr){
                        if( val == name ){
                            elements.push( children[i] );
                            break;
                        }
                    }
                    elements = _getElementsHaveAttr( children[i], name, elements );
                }
                return elements;
            }
            var elements:Array = [];
            elements = _getElementsHaveAttr( this, name, elements );
            return elements;
        }
        
        XMLNode.prototype.getElementsByAttrValue = function( name:String, value:String ):Array
        {
            function _getElementsByAttrValue( node:XMLNode, name:String , elements:Array ):Array
            {
                var children:Array = node.childNodes;
                var len:Number = children.length;
                for(var i=0;i<len;i++){
                    var attr:String = children[i].attributes;
                    for(var val in attr){
                        if( val == name && attr[val] == value ){
                            elements.push( children[i] );
                            break;
                        }
                    }
                    elements = _getElementsByAttrValue( children[i], name, elements );
                }
                return elements;
            }
            var elements:Array = [];
            elements = _getElementsByAttrValue( this, name, elements );
            return elements;
        }
        
    }
    
}

